package edu.yourl.web.components;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.spring.injection.annot.SpringBean;

import edu.yourl.application.YourlSession;
import edu.yourl.domain.Domain;
import edu.yourl.domain.Gossip;
import edu.yourl.domain.Gossiper;
import edu.yourl.domain.Url;
import edu.yourl.domain.User;
import edu.yourl.presenter.IGossipPresenter;
import edu.yourl.web.DomainPage;
import edu.yourl.web.GossiperPage;
import edu.yourl.web.UrlPage;
import edu.yourl.web.UserPage;

public class GossipListPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@SpringBean(name = "gossipPresenter")
	private IGossipPresenter presenter;

	@SuppressWarnings("serial")
	public GossipListPanel(String id, IDataProvider<Gossip> data) {
		super(id);
		add(new DataView<Gossip>("gossipList", data) {

			@Override
			protected void populateItem(Item<Gossip> item) {

				item.add(new GossipView("gossip", item.getModelObject()));
			}
		});
	}

	@SuppressWarnings("serial")
	private final class GossipView extends WebMarkupContainer implements
			IGossipView {

		private final class ReportBehavior extends
				AbstractDefaultAjaxBehavior {
			@Override
			protected void respond(AjaxRequestTarget target) {
				presenter.reportAbuse(GossipView.this);
				target.addComponent(GossipView.this);
			}

			@Override
			public CharSequence getCallbackScript() {
				return "if(confirm('Confirma reportar como abuso?')) " + super.getCallbackScript();
			}
		}

		private final Gossip gossip;
		private String htmlGossip;
		private ReportBehavior report;

		private GossipView(String id, Gossip gossip) {
			super(id);
			this.gossip = gossip;
			report = new ReportBehavior();
			add(report);
			setOutputMarkupId(true);
		}

		@Override
		protected void onComponentTagBody(MarkupStream markupStream,
				ComponentTag openTag) {

			presenter.renderGossip(gossip, this);

			htmlGossip += "<div class=\"by\"> por ";
			htmlGossip += getGossiperPageLink(gossip.getAuthor(),
					UserPage.class, gossip.getAuthor().getFavoritableName());
			htmlGossip += " hace " + presenter.getPrintableDate(gossip);
			if (!gossip.isAbuse()) { 
				htmlGossip += " - <a href=\"#\" onclick=\"" + report.getCallbackScript() + "\">Reportar abuso</a>";
			} else {
				htmlGossip += " - Reportado como abuso";
			}
			htmlGossip += "</div>";

			replaceComponentTagBody(markupStream, openTag, htmlGossip);
		}

		@Override
		public void clear() {
			htmlGossip = "";
		}

		@Override
		public void addText(String message) {
			htmlGossip += message;
		}

		@Override
		public void addUrl(Url gossiper) {

			htmlGossip += getGossiperPageLink(gossiper, UrlPage.class, gossiper
					.getFavoritableName());
			htmlGossip += " (";
			htmlGossip += "<a href=\"" + gossiper.getUrlWithProtocolCheck() + "\"><img src=\""
					+ getImagePath("img/follow_link.jpg")
					+ "\" width=\"15\" border=\"0\"/> Ir</a>";
			htmlGossip += " | ";
			Domain parent = gossiper.getParent();
			htmlGossip += getGossiperPageLink(parent, DomainPage.class,
					"<img src=\""
					+ getImagePath("img/domain.png")
					+ "\" width=\"15\" border=\"0\"/> Dominio");
			htmlGossip += ")";

		}

		private String getImagePath(String path) {
			return getRequestCycle().getProcessor().getRequestCodingStrategy()
					.rewriteStaticRelativeUrl(path);
		}

		@Override
		public void addUser(User gossiper) {
			htmlGossip += getGossiperPageLink(gossiper, UserPage.class,
					gossiper.getFavoritableName());
		}

		private String getGossiperPageLink(Gossiper gossiper,
				Class<? extends GossiperPage> pageClass, final String body) {

			PageParameters parameters = new PageParameters();
			String seoFriendlyId = gossiper.getSEOFriendlyId();
			parameters.add(GossiperPage.GOSSIPER_PARAMETER, seoFriendlyId);
			if (seoFriendlyId != null) {
				return "<a href=\""
						+ getRequestCycle().urlFor(pageClass, parameters)
						+ "\">" + body + "</a>";
			} else {
				return body;
			}
		}

		@Override
		public String getCurrentUserNick() {
			return ((YourlSession)super.getSession()).getUserNick();
		}

		@Override
		public Gossip getGossip() {
			return gossip;
		}
	}

}
